using System;
using Dto;
using UnityEngine;
using UnityEngine.UI;

namespace UI
{
    public class StatePanel : UIBase
    {
        protected Account user;
        protected Image _imgIdentity;
        protected Text _txtReady;
        protected Image _imgChat;
        protected Text _textChat;

        protected virtual void Start()
        {
            _imgIdentity = transform.Find("ImgIdentity").GetComponent<Image>();
            _txtReady = transform.Find("TxtReady").GetComponent<Text>();
            _imgChat = transform.Find("Imgchat").GetComponent<Image>();
            _textChat = _imgChat.transform.Find("Text").GetComponent<Text>();

            //默认状态
            _txtReady.gameObject.SetActive(false);
            _imgChat.gameObject.SetActive(false);
        }

        protected virtual void Awake()
        {
            Bind(UIEvent.PLAYER_READY, UIEvent.PLAYER_HIDE_STATE, UIEvent.PLAYER_LEAVE, UIEvent.PLAYER_ENTER,
                UIEvent.PLAYER_CHAT);
        }

        public override void Execute(int eventCode, object message)
        {
            switch (eventCode)
            {
                case UIEvent.PLAYER_READY:
                {
                    string userId = (string) message;
                    if (user !=null && user.id == userId)
                    {
                        ReadyState();
                    }

                    break;
                }
                case UIEvent.PLAYER_HIDE_STATE:
                    _txtReady.gameObject.SetActive(false);
                    break;
                case UIEvent.PLAYER_LEAVE:
                {
                    string userId = (string) message;
                    if (user == null)
                    {
                        break;
                    }

                    if (user.id == userId)
                    {
                        setPanelActive(false);
                    }

                    break;
                }
                case UIEvent.PLAYER_ENTER:
                {
                    string userId = (string) message;
                    if (user == null)
                    {
                        break;
                    }

                    if (user.id == userId)
                    {
                        setPanelActive(true);
                    }

                    break;
                }
                case UIEvent.PLAYER_CHAT:
                {
                    if (user == null)
                        break;
                    ChatDto chat = message as ChatDto;
                    if (user.id.Equals(chat.userId))
                    {
                        ShowChat(chat.text);
                    }

                    break;
                }
                default:
                    break;
            }
        }

        protected virtual void ReadyState()
        {
            _txtReady.gameObject.SetActive(true);
        }

        protected void SetIdentity(int identity)
        {
            if (identity == 0)
            {
                _imgIdentity.sprite = Resources.Load<Sprite>("Identity/Farmer");
            }
            else if (identity == 1)
            {
                _imgIdentity.sprite = Resources.Load<Sprite>("Identity/Landlord");
            }
        }

        /**
         * 显示时间
         */
        protected int showTime = 3;

        /**
         * 计时器
         */
        protected float timer = 0f;

        /**
         * 是否显示
         */
        protected bool isShow = false;

        protected virtual void Update()
        {
            if (isShow)
            {
                timer += Time.deltaTime;
                if (timer >= showTime)
                {
                    SetChatActive(false);
                    timer = 0f;
                    isShow = false;
                }
            }
        }

        protected void SetChatActive(bool active)
        {
            _imgChat.gameObject.SetActive(active);
        }

        /**
         * 外界调用的 显示聊天
         */
        protected void ShowChat(string text)
        {
            // 设置文字
            _textChat.text = text;
            // 显示动画
            SetChatActive(true);
            isShow = true;
        }
    }
}